/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvTypeprime;
import fr.cggveritas.crewintranet.jms.entities.CvTravail;
import fr.cggveritas.crewintranet.jms.entities.CvTravailprime;
import fr.cggveritas.crewintranet.jms.entities.CvTravailprimePK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvTravailprimeJpaController implements Serializable {

    public CvTravailprimeJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CvTravailprime cvTravailprime) throws PreexistingEntityException, Exception {
        if (cvTravailprime.getCvTravailprimePK() == null) {
            cvTravailprime.setCvTravailprimePK(new CvTravailprimePK());
        }
        cvTravailprime.getCvTravailprimePK().setTravailleurId(cvTravailprime.getCvTravail().getCvTravailleurId());
        cvTravailprime.getCvTravailprimePK().setTypeprime(cvTravailprime.getCvTypeprime().getTypeprime());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypeprime cvTypeprime = cvTravailprime.getCvTypeprime();
            if (cvTypeprime != null) {
                cvTypeprime = em.getReference(cvTypeprime.getClass(), cvTypeprime.getTypeprime());
                cvTravailprime.setCvTypeprime(cvTypeprime);
            }
            CvTravail cvTravail = cvTravailprime.getCvTravail();
            if (cvTravail != null) {
                cvTravail = em.getReference(cvTravail.getClass(), cvTravail.getCvTravailleurId());
                cvTravailprime.setCvTravail(cvTravail);
            }
            em.persist(cvTravailprime);
            if (cvTypeprime != null) {
                cvTypeprime.getCvTravailprimeList().add(cvTravailprime);
                cvTypeprime = em.merge(cvTypeprime);
            }
            if (cvTravail != null) {
                cvTravail.getCvTravailprimeList().add(cvTravailprime);
                cvTravail = em.merge(cvTravail);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCvTravailprime(cvTravailprime.getCvTravailprimePK()) != null) {
                throw new PreexistingEntityException("CvTravailprime " + cvTravailprime + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvTravailprime cvTravailprime) throws NonexistentEntityException, Exception {
        cvTravailprime.getCvTravailprimePK().setTravailleurId(cvTravailprime.getCvTravail().getCvTravailleurId());
        cvTravailprime.getCvTravailprimePK().setTypeprime(cvTravailprime.getCvTypeprime().getTypeprime());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTravailprime persistentCvTravailprime = em.find(CvTravailprime.class, cvTravailprime.getCvTravailprimePK());
            CvTypeprime cvTypeprimeOld = persistentCvTravailprime.getCvTypeprime();
            CvTypeprime cvTypeprimeNew = cvTravailprime.getCvTypeprime();
            CvTravail cvTravailOld = persistentCvTravailprime.getCvTravail();
            CvTravail cvTravailNew = cvTravailprime.getCvTravail();
            if (cvTypeprimeNew != null) {
                cvTypeprimeNew = em.getReference(cvTypeprimeNew.getClass(), cvTypeprimeNew.getTypeprime());
                cvTravailprime.setCvTypeprime(cvTypeprimeNew);
            }
            if (cvTravailNew != null) {
                cvTravailNew = em.getReference(cvTravailNew.getClass(), cvTravailNew.getCvTravailleurId());
                cvTravailprime.setCvTravail(cvTravailNew);
            }
            cvTravailprime = em.merge(cvTravailprime);
            if (cvTypeprimeOld != null && !cvTypeprimeOld.equals(cvTypeprimeNew)) {
                cvTypeprimeOld.getCvTravailprimeList().remove(cvTravailprime);
                cvTypeprimeOld = em.merge(cvTypeprimeOld);
            }
            if (cvTypeprimeNew != null && !cvTypeprimeNew.equals(cvTypeprimeOld)) {
                cvTypeprimeNew.getCvTravailprimeList().add(cvTravailprime);
                cvTypeprimeNew = em.merge(cvTypeprimeNew);
            }
            if (cvTravailOld != null && !cvTravailOld.equals(cvTravailNew)) {
                cvTravailOld.getCvTravailprimeList().remove(cvTravailprime);
                cvTravailOld = em.merge(cvTravailOld);
            }
            if (cvTravailNew != null && !cvTravailNew.equals(cvTravailOld)) {
                cvTravailNew.getCvTravailprimeList().add(cvTravailprime);
                cvTravailNew = em.merge(cvTravailNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                CvTravailprimePK id = cvTravailprime.getCvTravailprimePK();
                if (findCvTravailprime(id) == null) {
                    throw new NonexistentEntityException("The cvTravailprime with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(CvTravailprimePK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTravailprime cvTravailprime;
            try {
                cvTravailprime = em.getReference(CvTravailprime.class, id);
                cvTravailprime.getCvTravailprimePK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvTravailprime with id " + id + " no longer exists.", enfe);
            }
            CvTypeprime cvTypeprime = cvTravailprime.getCvTypeprime();
            if (cvTypeprime != null) {
                cvTypeprime.getCvTravailprimeList().remove(cvTravailprime);
                cvTypeprime = em.merge(cvTypeprime);
            }
            CvTravail cvTravail = cvTravailprime.getCvTravail();
            if (cvTravail != null) {
                cvTravail.getCvTravailprimeList().remove(cvTravailprime);
                cvTravail = em.merge(cvTravail);
            }
            em.remove(cvTravailprime);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvTravailprime> findCvTravailprimeEntities() {
        return findCvTravailprimeEntities(true, -1, -1);
    }

    public List<CvTravailprime> findCvTravailprimeEntities(int maxResults, int firstResult) {
        return findCvTravailprimeEntities(false, maxResults, firstResult);
    }

    private List<CvTravailprime> findCvTravailprimeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvTravailprime.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvTravailprime findCvTravailprime(CvTravailprimePK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvTravailprime.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvTravailprimeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvTravailprime> rt = cq.from(CvTravailprime.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
